const express = require('express');
// 用来解析请求体中的fromDate类型的数据
const multer = require('multer');
const fs = require('fs');
const app = express();
app.use(express.static('www'));
app.use(express.static('www'));



/* // 文件储存的配置对象
var storage = multer.diskStorage({
    destination: 'www/uploads', // 储存路径
    filename: function (req, file, cb) { // 文件名称
        cb(null, fill.fieldname+'.webp')
    }
}) */

// 根据配置信息生成一个处理对象
// const upload = multer({ storage })
// single()单个文件上传的情况
/*  const upload = multer({dest:'www/uploads'});
app.post('/upload', upload.array(), (req, res) => {
    console.log(req.body);
    console.log(req.files);
    res.send('内容');
}) */

const upload = multer({ dest: 'www/uploads' })
// .array('myfile') 有多个文件上传时获取多个文件用的
app.post('/upload', upload.single('myfile'), (req, res) => {

    var old = `www/uploads/${req.file.filename}`;
    var ne = `www/uploads/${req.file.originalname}`;
    fs.rename(old, ne, (err) => {
        if (err) {
            console.log('更改失败');
        }
    })
    res.send('内容')
})


app.listen('3000', function () {
    console.log('这是项目一,端口:3000');
})
